<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="depts" :props="defaultProps" default-expand-all :expand-on-click-node="false">
        <template #default="{ data }">
          <el-row type="flex">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <div>
                <!-- 管理员 -->
                <span class="tree-manager">{{ data.managerName }}</span>
                <!-- 操作栏 -->
                <Operate :node="data" @add-dept="addDept" @edit-dept="editDept" @del-dept="delDept" />
              </div>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 使用添加部门的弹窗 -->
    <DeptDialogVues ref="deptRef" :show-dialog.sync="showDialog" :pid="pid" @update-list="render" @edit-dept="editDept" />
  </div>
</template>
<script>
import DeptDialogVues from './components/dept-dialog.vue'
import Operate from './components/operate.vue'
import { listTree } from '@/utils/index'
import { getCompanyDepartment } from '@/api/department/department'
export default {
  name: 'Department',
  components: { Operate, DeptDialogVues },
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      pid: 0
    }
  },
  async created() {
    await this.render()
  },
  methods: {
    addDept(id) {
      this.showDialog = true
      this.pid = id
    },
    offdialog() {
      this.showDialog = false
    },
    async render() {
      const res = await getCompanyDepartment()
      this.depts = listTree(res, 0)
    },
    async editDept(id) {
      this.pid = id
      await this.$refs.deptRef.getDetail(id)
      this.showDialog = true
    },
    delDept(id) {
      this.$refs.deptRef.delDetail(id)
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
  .el-row {
    width: 100%;
  }
  .tree-manager {
    margin-right: 10px;
  }
  .text-manager {
    margin-right: 20px;
  }
}
</style>
